package com.dao;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.FactoryBean;

public class MapperFactoryBean implements FactoryBean<Object> {
    private Class<?> mapperClass;
    private SqlSessionFactory factory;

    public SqlSessionFactory getFactory() {
        return factory;
    }

    public void setFactory(SqlSessionFactory factory) {
        this.factory = factory;
    }

    public Class<?> getMapperClass() {
        return mapperClass;
    }

    public void setMapperClass(Class<?> mapperClass) {
        this.mapperClass = mapperClass;
    }

    @Override
    public Object getObject() throws Exception {
        SqlSession sqlSession = getFactory().openSession(true);
        Object mapper = sqlSession.getMapper(getMapperClass());
        return mapper;

    }

    @Override
    public Class<?> getObjectType() {
        return mapperClass;
    }
}
